package com.zhihuitong.meeting.login;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhihuitong.meeting.R;
import com.zhihuitong.meeting.base.BaseMVPActivity;
import com.zhihuitong.meeting.global.MyApplication;
import com.zhihuitong.meeting.meeting.list.MainActivity;
import com.zhihuitong.meeting.utils.AppUtils;
import com.zhihuitong.meeting.utils.CheckedVersionUtil;
import com.zhihuitong.meeting.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

public class LoginActivity extends BaseMVPActivity<LoginPresenter> implements LoginContract.ILoginView {


    @BindView(R.id.img_logo)
    ImageView mImgLogo;
    @BindView(R.id.tv_description)
    TextView mTvDescription;
    @BindView(R.id.et_userName)
    EditText mEtUserName;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.btn_login)
    Button mBtnLogin;
    @BindView(R.id.btn_re)
    Button mBtnRe;
    @BindView(R.id.et_userId)
    EditText mEtUserId;
    @BindView(R.id.btn_get)
    Button mBtnGet;
    @BindView(R.id.ll_test)
    LinearLayout mLlTest;

    public static void start(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

    /**
     * 返回子类布局id
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    /**
     * 初始化數據
     */
    @Override
    protected void initData() {
        //  PgyUtils.checkVersion(mContext);
        CheckedVersionUtil.init(mContext);
    }

    /**
     * 初始化View
     */
    @Override
    protected void initView() {
        Log.i("===", "LoginActivity");
        String localUserName = MyApplication.getLocalUserName();
        String localAccount = MyApplication.getLocalAccount();
        String localPwd = MyApplication.getLocalPwd();
        if (!TextUtils.isEmpty(localUserName)) {
            mEtUserName.setText(localUserName);
        }
        if (!TextUtils.isEmpty(localAccount)) {
            mEtUserId.setText(localAccount);
        }
        if (!TextUtils.isEmpty(localPwd)) {
            mEtPwd.setText(localPwd);
        }
        mTvDescription.setText("vsersion: " + AppUtils.getVersionName(mContext) + "_" + AppUtils.getVersionCode(mContext));
    }


    private String getUserId() {
        return mEtUserId.getText().toString().trim();
    }

    private String getUserName() {
        return mEtUserName.getText().toString().trim();
    }

    private String getPassWord() {
        return mEtPwd.getText().toString().trim();
    }


    /**
     * MVP模式下創建Presenter 必須實現
     *
     * @return
     */
    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }


    @OnClick({R.id.btn_login, R.id.btn_re, R.id.btn_get, R.id.img_logo})
    public void onViewClicked(View view) {
        final String userId = getUserId();
        final String passWord = getPassWord();
        final String userName = getUserName();
        if (TextUtils.isEmpty(userId)) {
            ToastUtils.showToast("请输入用户名!");
            return;
        }

        if (TextUtils.isEmpty(passWord)) {
            ToastUtils.showToast("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showToast("请输入参会昵称");
            return;
        }

        switch (view.getId()) {
            case R.id.btn_login:
                showLoading();
                mBtnLogin.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPresenter.login(userId, passWord, userName);

                    }
                }, 800);
                break;
            case R.id.btn_re:

                mPresenter.releaseUser(userName, passWord);
                break;
            case R.id.btn_get:

                mPresenter.getUser(userName, passWord);
                break;
            case R.id.img_logo:
                test++;
                if (test >= 5) {
                    mLlTest.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    int test = 0;

    @Override
    public void loginSuc(String account, String pwd) {
        hidLoading();
        ToastUtils.showToast("登录成功");
        MainActivity.start(mContext);
        finish();
    }

    @Override
    public void loginFail(String msg) {
        hidLoading();
        ToastUtils.showToast("登录失败");
    }


}
